package com.dji.sdk.extensions.java.lang.Iterable;

import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;

import java.util.ArrayList;
import java.util.Collection;
import java.util.NoSuchElementException;
import java.util.function.IntFunction;
import java.util.function.Predicate;

/**
 * @author 吴昊
 */
@Extension
public class IterableExtension {
    public static <T> T[] toArray(@This Iterable<T> iterable, IntFunction<T[]> generator) {
        Collection<T> collection;
        if (iterable instanceof Collection) {
            collection = (Collection<T>) iterable;
        } else {
            collection = new ArrayList<>();
            iterable.forEach(collection::add);
        }
        return collection.toArray(generator.apply(collection.size()));
    }
}
